package Main;

import javax.swing.*;

import ChessBoard.Bishop;
import ChessBoard.Figure;
import ChessBoard.King;
import ChessBoard.Knight;
import ChessBoard.Pawn;
import ChessBoard.Queen;
import ChessBoard.Rook;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class ChessInterface {

	JFrame frame;
	JPanel squares[][] = new JPanel[8][8];
	public boolean makeNextMove = false;

	public ChessInterface() {
		frame = new JFrame("Simplified Chess");
		frame.setSize(800, 800);
		frame.setLayout(new GridLayout(8, 8));

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				squares[i][j] = new JPanel();

				if ((i + j) % 2 == 0) {
					squares[i][j].setBackground(Color.DARK_GRAY);
				} else {
					squares[i][j].setBackground(Color.white);
				}
				frame.add(squares[i][j]);
			}
		}

		squares[0][0].add(new JLabel(new ImageIcon("bRook.png")));
		squares[0][1].add(new JLabel(new ImageIcon("bKnight.png")));
		squares[0][2].add(new JLabel(new ImageIcon("bBishop.png")));
		squares[0][4].add(new JLabel(new ImageIcon("bKing.png")));
		squares[0][3].add(new JLabel(new ImageIcon("bQueen.png")));
		squares[0][5].add(new JLabel(new ImageIcon("bBishop.png")));
		squares[0][6].add(new JLabel(new ImageIcon("bKnight.png")));
		squares[0][7].add(new JLabel(new ImageIcon("bRook.png")));

		squares[7][0].add(new JLabel(new ImageIcon("wRook.png")));
		squares[7][1].add(new JLabel(new ImageIcon("wKnight.png")));
		squares[7][2].add(new JLabel(new ImageIcon("wBishop.png")));
		squares[7][4].add(new JLabel(new ImageIcon("wKing.png")));
		squares[7][3].add(new JLabel(new ImageIcon("wQueen.png")));
		squares[7][5].add(new JLabel(new ImageIcon("wBishop.png")));
		squares[7][6].add(new JLabel(new ImageIcon("wKnight.png")));
		squares[7][7].add(new JLabel(new ImageIcon("wRook.png")));

		for (int i = 0; i < 8; i++) {
			squares[1][i].add(new JLabel(new ImageIcon("bPawn.png")));
			squares[6][i].add(new JLabel(new ImageIcon("wPawn.png")));
		}

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		addEnterKeyListener();
	}

	public void movePiece(int oldx, int oldy, int x, int y, Figure f) {
		oldx = oldx - 1;
		oldy = 7 - (oldy - 1);
		x = x - 1;
		y = 7 - (y - 1);
		squares[oldy][oldx].removeAll();
		squares[oldy][oldx].setVisible(false);
		squares[oldy][oldx].setVisible(true);
		if (squares[y][x].getComponentCount() == 1) {
			squares[y][x].removeAll();
		}
		if (f instanceof Pawn) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wPawn.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bPawn.png")));
		} else if (f instanceof Knight) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wKnight.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bKnight.png")));
		} else if (f instanceof Bishop) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wBishop.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bBishop.png")));
		} else if (f instanceof Rook) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wRook.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bRook.png")));
		} else if (f instanceof Queen) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wQueen.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bQueen.png")));
		} else if (f instanceof King) {
			if (f.isWhite)
				squares[y][x].add(new JLabel(new ImageIcon("wKing.png")));
			else
				squares[y][x].add(new JLabel(new ImageIcon("bKing.png")));
		}
	}

	private void addEnterKeyListener() {
		frame.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar() == e.VK_ENTER) {
					makeNextMove=true;
				}
			}

			// unused abstract methods
			public void keyTyped(KeyEvent e) {
			}

			public void keyReleased(KeyEvent e) {
			}

		});
	}
}